package com.wpen.unit.service.job.impl;

import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wpen.unit.cvt.job.AgentExec1LogCvt;
import com.wpen.unit.infra.mybatis.job.po.AgentExec1LogPo;
import com.wpen.unit.infra.repository.job.AgentExec1LogRepository;
import com.wpen.unit.service.job.AgentExecLogService;
import com.wpen.unit.utils.DateUtil;
import com.wpen.unit.web.dto.job.AgentExec1LogDetailRequest;
import com.wpen.unit.web.dto.job.AgentExec1LogDetailResponse;
import com.wpen.unit.web.dto.job.AgentExec1LogListRequest;
import com.wpen.unit.web.dto.job.AgentExec1LogListResponse;
import com.wpen.unit.web.dto.job.AgentExec1LogListRowsResponse;
import com.wpen.unit.web.dto.job.StatRequest;
import com.wpen.unit.web.dto.job.StatResponse;

/**
 * 任务执行器日志业务处理.
 * 
 * @author Wang Peng
 * @date 2022-12-09
 */
@Service
public class AgentExecLogServiceImpl implements AgentExecLogService {

	@Autowired
	private AgentExec1LogRepository agentExecLogRepository;

	@Override
	public StatResponse statTodayAgentExec(StatRequest statRequest) {
		StatResponse statResponse = new StatResponse();
		// 数据库查询
		AgentExec1LogPo agentExecLogPo = new AgentExec1LogPo();
		agentExecLogPo.setExecDate(DateUtil.format(DateUtil.FORMAT_DATE2));
		long total = agentExecLogRepository.count(agentExecLogPo);
		statResponse.setCount(total);
		agentExecLogPo.setExecDate(DateUtil.offset(Calendar.DAY_OF_YEAR, -1, DateUtil.FORMAT_DATE2));
		long total2 = agentExecLogRepository.count(agentExecLogPo);
		statResponse.setAlive(total2);
		return statResponse;
	}

	@Override
	public AgentExec1LogListResponse queryListAgentExecLog(AgentExec1LogListRequest agentExecLogListRequest) {
		int index = agentExecLogListRequest.getIndex();
		int size = agentExecLogListRequest.getSize();
		// 数据库查询
		AgentExec1LogPo agentExecLogPo = AgentExec1LogCvt.INSTANCE.queryListDto2Do(agentExecLogListRequest);

		long total = agentExecLogRepository.count(agentExecLogPo);
		List<AgentExec1LogPo> agentExecLogPoLists = agentExecLogRepository.queryList(index, size, agentExecLogPo);

		// 返回响应信息
		AgentExec1LogListResponse agentExecLogListResponse = new AgentExec1LogListResponse();
		agentExecLogListResponse.setTotal(total);
		agentExecLogListResponse.setCount(0);
		if (agentExecLogPoLists != null) {
			agentExecLogListResponse.setCount(agentExecLogPoLists.size());
			List<AgentExec1LogListRowsResponse> listRows = agentExecLogListResponse.getRows();
			for (AgentExec1LogPo inAgentExecLogPo : agentExecLogPoLists) {
				AgentExec1LogListRowsResponse row = AgentExec1LogCvt.INSTANCE.queryListRowDo2Dto(inAgentExecLogPo);
				row.setStartupTime(DateUtil.format(inAgentExecLogPo.getStartupTime(), DateUtil.FORMAT_DATETIME));
				if (inAgentExecLogPo.getEndTime() != null) {
					row.setEndTime(DateUtil.format(inAgentExecLogPo.getEndTime(), DateUtil.FORMAT_DATETIME));
				}
				listRows.add(row);
			}
			agentExecLogListResponse.setRows(listRows);
		}

		return agentExecLogListResponse;
	}

	/**
	 * 任务执行器1日志-明细查询
	 * 
	 * @param agentExec1LogDetailRequest
	 * @return
	 */

	@Override
	public AgentExec1LogDetailResponse detailAgentExec1Log(AgentExec1LogDetailRequest agentExec1LogDetailRequest) {
		// 数据库查询
		Long id = agentExec1LogDetailRequest.getId();
		Long jobId = agentExec1LogDetailRequest.getJobExecLogId();
		Long triggerTime = agentExec1LogDetailRequest.getTaskTriggerTime();
		AgentExec1LogPo agentExec1LogPoDb = null;
		if (id != null && id > 0) {
			agentExec1LogPoDb = agentExecLogRepository.queryById(id);
		} else {
			agentExec1LogPoDb = agentExecLogRepository.queryByJobId(jobId, triggerTime);
		}
		if (agentExec1LogPoDb == null) {
			return new AgentExec1LogDetailResponse();
		}

		// 返回响应信息
		AgentExec1LogDetailResponse agentExec1LogDetailResponse = AgentExec1LogCvt.INSTANCE
				.detailDo2Dto(agentExec1LogPoDb);
		return agentExec1LogDetailResponse;
	}

}
